package com.icongyou.enterprise.talent_analysis.scheduler;

import com.icongyou.enterprise.talent_analysis.mapper.AIInsightCacheMapper;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;

import static org.mockito.Mockito.*;

/**
 * 缓存清理调度器测试
 */
@ExtendWith(MockitoExtension.class)
@DisplayName("缓存清理调度器测试")
class CacheCleanupSchedulerTest {

    @Mock
    private AIInsightCacheMapper aiInsightCacheMapper;

    @InjectMocks
    private CacheCleanupScheduler scheduler;

    @Test
    @DisplayName("cleanupExpiredAIInsightCache - 成功清理过期缓存")
    void testCleanupExpiredAIInsightCache_Success() {
        // Arrange
        when(aiInsightCacheMapper.deleteExpired()).thenReturn(10);

        // Act
        scheduler.cleanupExpiredAIInsightCache();

        // Assert
        verify(aiInsightCacheMapper).deleteExpired();
    }

    @Test
    @DisplayName("cleanupExpiredAIInsightCache - 无过期缓存")
    void testCleanupExpiredAIInsightCache_NothingToDelete() {
        // Arrange
        when(aiInsightCacheMapper.deleteExpired()).thenReturn(0);

        // Act
        scheduler.cleanupExpiredAIInsightCache();

        // Assert
        verify(aiInsightCacheMapper).deleteExpired();
    }

    @Test
    @DisplayName("cleanupExpiredAIInsightCache - 异常不中断调度")
    void testCleanupExpiredAIInsightCache_Exception() {
        // Arrange
        when(aiInsightCacheMapper.deleteExpired())
            .thenThrow(new RuntimeException("数据库错误"));

        // Act - 不应抛出异常
        scheduler.cleanupExpiredAIInsightCache();

        // Assert
        verify(aiInsightCacheMapper).deleteExpired();
    }
}
